/*************************************************************************
 *     >      File Name: configini.c
 *     >    Description: This file is write for parsering configuration file.
 *
 *     >         Author: Yang Xiaohan
 *     >           Mail: yangxiaohan_niki@163.com
 *     >   Created Time: Fri 22 Jul 2022 10:24:28 CST
 *     >   Changed Time: Fri 22 Jul 2022 10:24:28 CST
 **************************************************************************/

#include <stdio.h>
#include <string.h>
#include "iniparser.h"
#include "configini.h"


int get_config(char *path,struct mqtt_user_data *mqtt, int mode)
{
	dictionary			*ini = NULL;
	
	const char			*brokeraddress;
	int					 brokerport;
	const char			*username;
	const char 			*password;
	const char      	*client_id;
	const char			*topic;
	int			 		QOS;
	int					KEEP_ALIVE;
	const char			*platform;

	const char			*type;
	const char			*state;
	const char			*clientToken;
	
	const char   		*version;
	const char			*method;
	const char			*id;
	const char			*identifier;


	if(!path||!mqtt)
	{
		printf("invalid_argument:%s\n",__FUNCTION__);
		return -1;
	}
	ini = iniparser_load(path);

	if(ini == NULL)
	{
		printf("iniparser_load failure!\n");
		return -2;
	}

	brokeraddress = iniparser_getstring(ini,"address:host","NULL");
	brokerport = iniparser_getint(ini, "address:port",-1);
	username = iniparser_getstring(ini,"username_pw:username","NULL");
	password = iniparser_getstring(ini,"username_pw:password","NULL");
	client_id =iniparser_getstring(ini,"client_id:client_id","NULL");
	QOS = iniparser_getint(ini, "QOS:QOS",-1);
	KEEP_ALIVE = iniparser_getint(ini,"KEEP_ALIVE:KEEP_ALIVE",-1);
	platform = iniparser_getstring(ini,"platform:platform","NULL");

	if(mode == SUB)
	{
		topic = iniparser_getstring(ini,"sub_topic:topic","NULL");
	}
	else if(mode == PUB)
	{
		topic = iniparser_getstring(ini, "pub_topic:topic","NULL");
		type = iniparser_getstring(ini,"json:type","NULL");
		state = iniparser_getstring(ini,"json:state","NULL");
		clientToken = iniparser_getstring(ini,"json:clientToken","NULL");
		
		version = iniparser_getstring(ini,"json:version","NULL");
		identifier = iniparser_getstring(ini,"json:identifier","NULL");
		id = iniparser_getstring(ini,"json:id","NULL");
		method = iniparser_getstring(ini,"json:method","NULL");
		

	}
	else if(mode != SUB && mode != PUB)
	{
		printf("invalid_argument: %s mode!\n",__FUNCTION__);
		return -3;
	}

	strncpy(mqtt->brokeraddress,brokeraddress,SIZE);
	mqtt->brokerport = brokerport;
	mqtt->QOS = QOS;
	mqtt->KEEP_ALIVE =KEEP_ALIVE;
	strncpy(mqtt->username,username,SIZE);
	strncpy(mqtt->password,password,SIZE);
	strncpy(mqtt->client_id,client_id,SIZE);
	strncpy(mqtt->topic,topic,SIZE);
	strncpy(mqtt->platform,platform,SIZE);

	if(mode == PUB)
	{
		strncpy(mqtt->type,type,SIZE);
		strncpy(mqtt->state,state,SIZE);
		strncpy(mqtt->clientToken,clientToken,SIZE);

		strncpy(mqtt->version,version,SIZE);
		strncpy(mqtt->id,id,SIZE);
		strncpy(mqtt->identifier,identifier,SIZE);
		strncpy(mqtt->method,method,SIZE);
	}
/*	
	printf("adress:%s\n",mqtt->brokeraddress);
	printf("port:%d\n",mqtt->brokerport);
	printf("topic:%s\n",mqtt->topic);
	printf("client_id:%s\n",mqtt->client_id);
	printf("password:%s\n",mqtt->password);
	printf("KEEP_ALIVE:%d\n",mqtt->KEEP_ALIVE);
	printf("QOS:%d\n",mqtt->QOS);
	printf("version:%s\n",mqtt->version);
	printf("platform:%s\n",mqtt->platform);
	printf("id:%s\n",mqtt->id);
	printf("identifier:%s\n",mqtt->identifier);
	printf("method:%s\n",mqtt->method);
*/
	iniparser_freedict(ini);
	return 0;
	
}

#if 0  
int main (int argc, char **argv)
{
	int 						rv = -1;
	int							port;
	char						*address = NULL;
	mqtt_user_data  			mqtt;

	memset(&mqtt, 0, sizeof(mqtt));
	rv = get_config(PATH,&mqtt,PUB);
	if(rv)
	{
		printf("get_config() failure!\n");
		return -1;
	}

	address = mqtt.brokeraddress;
	port = mqtt.brokerport;
	
	printf("brokeraddress:%s\n",address);
	printf("brokrtport:%d\n",port);
	return 0;
} 





int main (int argc, char **argv)
{
	FILE 			*fp = NULL;
	dictionary		*ini = NULL;



	ini = iniparser_load(PATH);
	if( ini = NULL)
	{
		printf("iniparser file failure\n");
		return -1;
	}
	iniparser_set(ini, "address:port","9999");

	printf("%s\n",iniparser_getstring(ini,"address:host","null"));
	printf("%s\n",iniparser_getstring(ini,"addressress:port","9999"));
	printf("%s\n",iniparser_getstring(ini,"username_pw:username","null"));
	printf("%s\n",iniparser_getstring(ini,"username_pw:password","null"));
	printf("%s\n",iniparser_getstring(ini,"client_id:client_id","null"));
	printf("%s\n",iniparser_getstring(ini,"pub_topic:topic","null"));
	printf("%s\n",iniparser_getstring(ini,"json:id","null"));
	printf("%s\n",iniparser_getstring(ini,"json:vesion","null"));
	printf("%s\n",iniparser_getstring(ini,"KEEP_ALIVE:KEEP_ALIVE","null"));
	printf("%s\n",iniparser_getstring(ini,"QOS:QOS","null"));

	fp = fopen(PATH, "w");
	if(fp == NULL)
	{
		printf("open file failure!\n");
		exit(-1);
	}

	iniparser_dumpsection_ini(ini, "address", fp);
	iniparser_dumpsection_ini(ini, "username_pw", fp);
	iniparser_dumpsection_ini(ini, "client_id", fp);
	iniparser_dumpsection_ini(ini, "pub_topic",fp);
	iniparser_dumpsection_ini(ini, "json", fp);
	iniparser_dumpsection_ini(ini, "KEEP_ALIVE", fp);
	iniparser_dumpsection_ini(ini, "QOS", fp);
	fclose(fp);

	iniparser_freedict(ini);
	return 0;
} 


#endif
